package com.sicheng.lc.周赛.分类.模拟;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/21 22:17
 */
public class 统计网格图中没有被保卫的格子数 {
    //https://leetcode.cn/problems/count-unguarded-cells-in-the-grid/
    public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
        int[][] nums = new int[m][n];
        init(guards, walls, nums);
        int res = 0;
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (nums[i][j] == 0) {
                    res++;
                }
            }
        }

        return res;
    }

    private void init(int[][] guards, int[][] walls, int[][] nums) {
        for (int[] guard : guards) {
            nums[guard[0]][guard[1]] = 2;
        }
        for (int[] guard : walls) {
            nums[guard[0]][guard[1]] = 2;
        }

        for (int[] guard : guards) {
            moni(nums, guard[0], guard[1]);
        }
    }

    private void moni(int[][] nums, int x, int y) {
        for (int i = y + 1; i < nums[x].length && nums[x][i] < 2; i++) {
            nums[x][i] = 1;
        }
        for (int i = y - 1; i >= 0 && nums[x][i] < 2; i--) {
            nums[x][i] = 1;
        }
        for (int i = 1; x + i < nums.length && nums[x + i][y] < 2; i++) {
            nums[x + i][y] = 1;
        }
        for (int i = 1; i <= x && nums[x - i][y] < 2; i++) {
            nums[x - i][y] = 1;
        }

    }
}
